local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local protocol = require(appdf.CLIENT_SRC.."gamemodel.protocol")

local FreeGoldLayer = class("FreeGoldLayer",function(scene)
    local alipay = display.newLayer(cc.c4b(0, 0, 0, 180))
	return alipay
end)

function FreeGoldLayer:ctor(scene)
    ExternalFun.registerNodeEvent(self)
    self._scene = scene
    
  --  self:init()
	local today = os.date("%d", os.time())
	local markday = cc.UserDefault:getInstance():getStringForKey("freegoldDay",0)
	if today ~= markday then
		GlobalUserItem.TodayAlmsCount = 0
		
	end
	self:setContentSize(yl.WIDTH,yl.HEIGHT)
	
	self:loadCSB()

end
function FreeGoldLayer:onExit()

end
-- 进入场景而且过渡动画结束时候触发。
function FreeGoldLayer:onEnterTransitionFinish()
    return self
end

-- 退出场景而且开始过渡动画时候触发。
function FreeGoldLayer:onExitTransitionStart()
	self:unregisterScriptTouchHandler()
    return self
end

function FreeGoldLayer:loadCSB()
    local csbnode = ExternalFun.loadCSB("popView/getfreeLayer.csb",self)
	self.csbnode = csbnode
	self.bg = csbnode:getChildByName("bg2_1")
	ExternalFun.FixScaleUI(self.bg,true)
	-- ExternalFun.FixTopUI(self.bg)
	ExternalFun.playOpenLayerAnimation( self.bg )
    self.btn_close = self.bg:getChildByName("btn_close")
    self.btn_sure = self.bg:getChildByName("togetBtn")


    local btnCallback = function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			self:onButtonClicked(ref:getTag(),ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)

		end
    end
    
    self.btn_close:addTouchEventListener(btnCallback)
    self.btn_sure:addTouchEventListener(btnCallback)

	self:init()
end

function FreeGoldLayer:init()
	local limitaltas = self.bg:getChildByName("limit")
	local cangettimealtas = self.bg:getChildByName("cangettime")
	local getcountAltas = self.bg:getChildByName("getcount")
	
	limitaltas:setString(GlobalUserItem.channelconfig.almslimit/appdf.bili)
	cangettimealtas:setString(GlobalUserItem.channelconfig.almsnum - GlobalUserItem.TodayAlmsCount)
	getcountAltas:setString(GlobalUserItem.channelconfig.almscount/appdf.bili)
end

function FreeGoldLayer:onButtonClicked(tag, ref)
	if ref == self.btn_close then
		self:removeFromParent()
	elseif ref == self.btn_sure then
			if GlobalUserItem.accounttype == 0 then
			showToast(self,"注册成为正式账号后即可每日免费领取！",2)
			return
		end
		local allmoney = GlobalUserItem.lUserScore+GlobalUserItem.lUserInsure	
		
		if allmoney >= GlobalUserItem.channelconfig.almslimit then
			showToast(self,"您今日还可领取"..((GlobalUserItem.channelconfig.almsnum - GlobalUserItem.TodayAlmsCount)).."次（金币不足"..GlobalUserItem.channelconfig.almslimit/appdf.bili.."时可领取）",2)
			return
		end	

		if GlobalUserItem.channelconfig.almsnum - GlobalUserItem.TodayAlmsCount == 0 then
			showToast(self,"今日免费金币已领完，请明日再来！",2)
			return
		end
		self._scene._LogonFrame:sendSocketData(protocol.CTRL_CLIENT_HALL,protocol.CXM_GETALMS)
		self._scene:showPopWait()
	end
end

return FreeGoldLayer